package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.*;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.common.validator.group.AddGroup;
import com.xhtt.common.validator.group.UpdateGroup;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.CfgComprod;
import com.xhtt.modules.cfg.service.CfgComprodService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 常用产品
 * </p>
 *
 * @author feipc
 * @since 2019-02-19
 */
@Api(value = "数据字典-常用产品", tags = "数据字典-常用产品")
@RestController
@RequestMapping("app/sys/cfg/comprod")
public class CfgComprodController extends AbstractController {

    @Autowired
    private CfgComprodService cfgComprodService;

    /**
     * 获取常用产品列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取常用产品列表(web/app)")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = cfgComprodService.queryPage(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增常用产品
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增常用产品(web)")
    @Login
    public R add(@RequestBody CfgComprod cfgComprod) {
        ValidatorUtils.validateEntity(cfgComprod, AddGroup.class);
        int count = cfgComprodService.count(new LambdaQueryWrapper<CfgComprod>().eq(CfgComprod::getName, cfgComprod.getName()));
        if (count > 0) {
            throw new RRException("产品名称已存在");
        }
        cfgComprodService.save(cfgComprod);
        return R.ok();
    }

    /**
     * 删除常用产品
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除常用产品(web)")
    @Login
    public R delete(@RequestBody Integer[] ids) {
        CfgComprod c = new CfgComprod();
        c.setStatus(-1);
        cfgComprodService.update(c, new UpdateWrapper<CfgComprod>().in(CfgComprod.ID, Arrays.asList(ids)));
        return R.ok();
    }

    /**
     * 修改常用产品
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改常用产品(web)")
    @Login
    public R update(@RequestBody CfgComprod cfgComprod) {
        ValidatorUtils.validateEntity(cfgComprod, UpdateGroup.class);
        cfgComprodService.updateComprod(cfgComprod);
        return R.ok();
    }

    /**
     * 获取常用产品
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取常用产品(web/app)")
    @Login
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgComprodService.getById(id));
    }

    @PostMapping(value = "/upload")
    @ApiOperation("常用产品导入")
    public R upload(MultipartFile file) {
        Map<Long, Map<Integer, String>> map = ReadExcel.readExcel(file, 1);
        cfgComprodService.upload(map);
        return R.ok();
    }

    @GetMapping(value = "/mouldExport")
    @ApiOperation("常用产品模板下载")
    public void mouldExport(HttpServletResponse response) {
        String fileName = "常用产品导入模板.xls";
        String path = "templates/outComprod.xls";
        DownloadFile.exportFile(fileName, path, response);
    }

}
